from glp.library import LabelInfo
from glp.library import DocumentMetadata

class Reader:
    @staticmethod
    def LabelTopics(label):
        if label == None: label = '-'
        info = LabelInfo.get_by_key_name(label)
        if info == None: return []
        topics = info.topics
        if topics == None: return []
        return topics

    @staticmethod
    def LabelsTopics(labels):
        if len(labels) == 0: return Reader.LabelTopics(None)
        topics = None
        for label in labels:
            if topics == None:
                topics = set(Reader.LabelTopics(label))
            else:
                topics &= set(Reader.LabelTopics(label))
        return list(topics)

    @staticmethod
    def LabelDocumentKeys(label):
        if label == None: label = '-'
        info = LabelInfo.get_by_key_name(label)
        if info == None: return []
        documents = info.documents
        if documents == None: return []
        return documents

    @staticmethod
    def LabelsDocumentKeys(labels):
        if len(labels) == 0: return Reader.LabelDocumentKeys(None)
        keys = None
        for label in labels:
            if keys == None:
                keys = set(Reader.LabelDocumentKeys(label))
            else:
                keys &= set(Reader.LabelDocumentKeys(label))
        return list(keys)

    @staticmethod
    def DocumentMetadata(id):
        return DocumentMetadata.get_by_key_name(id)
